import os
from datetime import datetime
import subprocess


date = datetime.now()

testDirName = "%d-%d-%d"%(date.day,date.month,date.year)

outputDir = "tests/%s/output/"%testDirName
errDir = "tests/%s/error/"%testDirName

if not os.path.exists(outputDir):
	os.makedirs(outputDir)

if not os.path.exists(errDir):
	os.makedirs(errDir)

for dirname, dirnames, filenames in os.walk('files'):
	for filename in filenames:
		if filename[-3:] == ".js":

			print os.path.join(dirname, filename)
			fullname = os.path.join(dirname, filename)

			cmd = "python jscompiler.py " + fullname

			outputFileName = os.path.join(outputDir,filename.replace(".js","")+"_salida.txt")
			errorFileName = os.path.join(errDir,filename.replace(".js","")+"_error.txt")

			outputFile = open(outputFileName,"w")
			errorFile = open(errorFileName,"w")

			return_value = subprocess.call(cmd,stdout=outputFile,stderr=errorFile,shell=True)
			print return_value
	
	# Advanced usage:
	# editing the 'dirnames' list will stop os.walk() from recursing into there.
	if '.svn' in dirnames:
		# don't go into any .git directories.
		dirnames.remove('.svn')
